package org.aeon

import org.aeon.entity.User
import org.aeon.service.BookService
import org.aeon.service.Login
import org.aeon.service.Register
import kotlin.system.exitProcess

/**
 * @author:     MissingAeon
 * @date:       2024/1/18 16:51
 * @description:
 */


fun main() {
    init()

    println("欢迎光临天天向上图书馆！")
    // 当前用户
    var user: User? = null

    var operation: Int?
    while (true) {
        println(
            """
        请输入相应的功能序号:
        1. 登录
        2. 注册
        3. 退出
        """.trimIndent()
        )
        try {
            operation = readln().toInt()
        } catch (e: NumberFormatException) {
            println("输入的非数字！")
            continue
        }
        when (operation) {
            1 -> user = Login().login()
            2 -> Register().prepare()
            3 -> exitProcess(0)
            else -> println("输入的序号非法！")
        }
        if (user != null) {
            while (true) {
                BookService.prompt()
                try {
                    operation = readln().toInt()
                    val bookService = BookService()
                    when (operation) {
                        // 添加图书
                        1 -> {
                            bookService.addBooks()
                        }
                        // 查询图书
                        2 -> {
                            bookService.searchBooks()
                            // 后续
                            bookService.followUp()?.let { it() }
                        }
                        // 修改图书
                        3 -> {
                            bookService.modifyBooks()
                        }
                        // 借阅图书
                        4 -> {
                            bookService.searchBooks()
                            bookService.borrowBooks(user!!)
                        }
                        // 查询已经借阅的图书
                        5 -> {
                            bookService.borrowed(user!!)
                        }
                        // 退还图书
                        6 -> {
                            bookService.borrowed(user!!)
                            bookService.returnBook(user)
                        }
                        // 退出当前账号
                        7 -> {
                            user = Login().logOut()
                            break
                        }

                        else -> throw IllegalArgumentException()
                    }
                } catch (e: NumberFormatException) {
                    println("输入的序号非法！")
                } catch (e: IllegalArgumentException) {
                    println("输入的序号非法！")
                }
            }
        }
    }
}

fun init() {

}
